package com.ease.plugin.metadata
{
	import com.ease.plugin.utils.PluginStrings;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	
     /**
	 * Signals that a new value has been added to the Metadata object.
	 * 
	 * @eventType org.osmf.events.MetadataEvent.VALUE_ADD
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion OSMF 1.0
	 */
     [Event(name='valueAdd', type='org.osmf.events.MetadataEvent')]
	
     /**
	 * Signals that a value has been removed from the Metadata object.
	 * 
	 * @eventType org.osmf.events.MetadataEvent.VALUE_REMOVE
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion OSMF 1.0
	 */
     [Event(name='valueRemove', type='org.osmf.events.MetadataEvent')]
	
     /**
	 * Signals that a value within the Metadata object has changed.
	 * 
	 * @eventType org.osmf.events.MetadataEvent.VALUE_CHANGE
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion OSMF 1.0
	 */
     [Event(name='valueChange', type='org.osmf.events.MetadataEvent')]
	
	/**
	 * Repository for metadata.  Metadata consists of key-value pairs,
	 * where keys are Strings and value are arbitrary objects.  This class
	 * provides a strongly-typed API for working with these key-value
	 * pairs, as well as events for detecting changes to the metadata.
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion OSMF 1.0
	 */  
	public class Metadata extends EventDispatcher
	{		
		/**
		 * Constructor.
		 * 
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion OSMF 1.0
		 */ 
		public function Metadata()		
		{						
		}
		
		/**
		 * Returns the value associate with the specified key.
		 * 
		 * Returns 'undefined' if the Metadata object fails to resolve the key.
		 * 
		 * @throws ArgumentError If key is null.
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion OSMF 1.0
		 */
		public function getValue(key:String):*
		{
			if (key == null)
			{
				throw new ArgumentError(PluginStrings.getString(PluginStrings.NULL_PARAM));
			}
			
			return data != null ? data[key] : null
		}
		
		/**
		 * Stores the specified value in this Metadata object, using the specified
		 * key.  The key can subsequently be used to retrieve the value.  If the
		 * key is equal to the key of another object already in the Metadata object
		 * this will overwrite the association with the new value.
		 * 
		 * @param key The key to associate the value with.
		 * @param value The value to add to the Metadata object.
		 * 
		 * @throws ArgumentError If key is null.
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion OSMF 1.0
		 */ 
		public function addValue(key:String, value:Object):void
		{
			if (key == null)
			{
				throw new ArgumentError(PluginStrings.getString(PluginStrings.NULL_PARAM));
			}
			
			if (data == null)
			{
				data = new Dictionary();
			}
			
			var oldValue:* = data[key];			
			data[key] = value;
			
			if (oldValue != value)
			{				
//				var event:Event
//					= oldValue === undefined
//						? new MetadataEvent
//							( MetadataEvent.VALUE_ADD
//							, false
//							, false
//							, key
//							, value
//							)
//						: new MetadataEvent
//							( MetadataEvent.VALUE_CHANGE
//							, false
//							, false
//							, key
//							, value
//							, oldValue
//							)
//						;
//						
//				dispatchEvent(event);
			}
		}
		
		/**
		 * Removes the value associated with the specified key from this
		 * Metadata object. Returns undefined if there is no value
		 * associated with the key in this Metadata object.
		 * 
		 * @param key The key associated with the value to be removed.
		 * @returns The removed item, null if no such item exists.
		 * 
		 * @throws ArgumentError If key is null.
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion OSMF 1.0
		 */ 
		public function removeValue(key:String):*
		{
			if (key == null)
			{
				throw new ArgumentError(PluginStrings.getString(PluginStrings.NULL_PARAM));
			}

			var value:* = data[key];
			if (value !== undefined)
			{
				delete data[key];
								
//				dispatchEvent
//					( new MetadataEvent
//						( MetadataEvent.VALUE_REMOVE
//						, false
//						, false
//						, key
//						, value
//						)
//					);
			}
			return value;
		}
		
		/**
		 * All of the keys stored in this Metadata object.
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion OSMF 1.0
		 */ 
		public function get keys():ArrayCollection
		{
			var allKeys:ArrayCollection = new ArrayCollection;
			if (data != null)
			{
				for (var key:Object in data)
				{
					allKeys.addItem(key);
				}
			}
			return allKeys;
		}

 				
		private var data:Dictionary;
	}
}